#ifndef mW_sprite_include
#define mW_sprite_include

#include <allegro.h>
#include <stdlib.h>
#include <string.h>
#include "game.h"
#include "list.h"
#include "lua.h"
#include "map.h"
#include "pathfinding.h"
#include "tile.h"
#include "utility.h"


#define DEBUG_SPRITE 0


// Number of pixels a sprite will move per frame
//   This should REALLY be the same as mW_MAP_SCROLL_SPEED
#define mW_SPRITE_SPEED 4

// Number of updates to display speech bubbles
#define mW_SPRITE_SPEECH_TIME 200


// values for the directions in mW_sprite->direction
#define mW_SPRITE_DIR_FRONT 0
#define mW_SPRITE_DIR_BACK  1
#define mW_SPRITE_DIR_LEFT  2
#define mW_SPRITE_DIR_RIGHT 3

// how many blit frames between sprite animation frames
#define mW_SPRITE_ANIMATE_DELAY 4




typedef struct
{
	
	// id of sprite from mW_sprites
	int id;
	
	// Sprite's position on the map
	unsigned short x;
	unsigned short y;
	
	// The destination the sprite is trying to reach
	// When these are equal to x,y then sprite is not moving
	unsigned short destination_x;
	unsigned short destination_y;
	
	// this is the offset from the current x,y position where the sprite
	//   should be drawn, because he is moving.
	int offset_x;
	int offset_y;
	
	// if the sprite is moving, this is the last computed path for it to
	//   reach its destination
	mW_list * path;
	
	// Name of sprite (what player sees)
	char * name;
	
	// The text the character is saying
	char * speech_text;
	
	// How many more updates to display text
	int speech_timer;
	
	// when this hits 0, we move to the next tile in this sprite's
	//   animation, and set animate_delay back to mW_SPRITE_ANIMATE_DELAY
	int animate_delay;
	
	// Direction the sprite is facing.
	unsigned char direction;
	
	// If this is not null, this map will be centered on this sprite as
	//   this sprite moves around
	mW_map * center_map;
	
	// Yes, every sprite has its own set of scripts and its own instance
	//   of lua
	lua_State * l_vm;
	
	// lists of (BITMAPS *), in the sequence of which they should be drawn
	mW_list * front;
	mW_list * back;
	mW_list * left;
	mW_list * right;
	mW_list * walk_front;
	mW_list * walk_back;
	mW_list * walk_left;
	mW_list * walk_right;
	
} mW_sprite;



mW_list * mW_sprites;

int mW_sprite_blit (BITMAP * surface, mW_sprite * sprite);
int mW_sprite_blit_all (BITMAP * surface);
int mW_sprite_blit_id (BITMAP * surface, int sprite_id);

/*
 * Purpose : Each time the sprite moves, it will scroll the map to center
 *           on the map. If NULL is passed instead of a map, the sprite
 *           will stop trying to center a map
 * Returns : 1 when complete
 * mW_error: If sprite_id is not a valid sprite_id
 */
int mW_sprite_center_map (int sprite_id, mW_map * map);
int mW_sprite_create ();
int mW_sprite_destroy (int sprite_id);
BITMAP * mW_sprite_get_tile (mW_sprite * sprite);
int mW_sprite_load_config (int sprite_id, char * config_filename);
int mW_sprite_init ();
int mW_sprite_move_to (int sprite_id, unsigned short x, unsigned short y);
int mW_sprite_update ();
int mW_sprite_next_location (mW_sprite * sprite);

int mW_sprite_lua_register(lua_State * l_vm);

int mW_sprite_lua_move_to (lua_State * l_vm);
int mW_sprite_lua_set_name (lua_State * l_vm);
int mW_sprite_lua_say (lua_State * l_vm);
int mW_sprite_lua_face_front (lua_State * l_vm);
int mW_sprite_lua_face_left (lua_State * l_vm);
int mW_sprite_lua_face_right (lua_State * l_vm);
int mW_sprite_lua_face_back (lua_State * l_vm);


#endif // mW_sprite_include
